#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

# ----------------------------------------------------------------- # 
# Models
# ----------------------------------------------------------------- # 
class Host(db.model):
    '''
    Represents a status host page
    '''
    name = db.StringProperty()
    twitter = db.StringProperty()
    hostname = db.StringProperty()
    updated = db.DateTimeProperty(auto_now_add=True)

# ----------------------------------------------------------------- # 
# Models
# ----------------------------------------------------------------- # 
class Status(db.model):
    '''
    Represents a status for a given web host
    '''
    date = db.DateTimeProperty(auto_now_add=True)
    message = db.TextProperty()

# ----------------------------------------------------------------- # 
# Request Handlers
# ----------------------------------------------------------------- # 
class IndexHandler(webapp.RequestHandler):

    def get(self):
        '''
        The main landing page
        '''
        hosts = Host.all()
        self.response.out.write('can phone')

    def post(self):
        '''
        When a user creates a new status page
        '''
        host = Host()
        host.name = self.request.get('name')
        host.twitter = self.request.get('twitter')
        host.hostname = self.request.get('hostname')
        host.put()

        self.response.out.write('can phone')
        self.redirect('/')

class StatusHandler(webapp.RequestHandler):

    def get(self):
        host = Host.gql("WHERE name = :1", hostname)
        self.response.out.write('can phone')

    def post(self):
        '''
        When a user creates a new status page
        '''
        host = Host.gql("WHERE name = :1", hostname)
        host.name = self.request.get('name')
        host.twitter = self.request.get('twitter')
        host.hostname = self.request.get('hostname')
        host.put()

        self.response.out.write('can phone')
        self.redirect('/')

# ----------------------------------------------------------------- # 
# Main launcher
# ----------------------------------------------------------------- # 
def main():
    application = webapp.WSGIApplication(
        [('/', IndexHandler)],
        [('/', StatusHandler)],
        debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
